/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.clientpackets;

import jmmos.Config;
import jmmos.gameserver.datatables.GuildTable;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.MMOWorld;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.network.SystemMessageId;
import jmmos.gameserver.network.serverpackets.ActionFailed;
import jmmos.gameserver.network.serverpackets.SystemMessage;

import gnu.trove.procedure.TObjectProcedure;

public final class RequestStartPledgeWar extends MMOGameClientPacket
{
	private static final String _C__03_REQUESTSTARTPLEDGEWAR = "[C] 03 RequestStartPledgewar";
	
	private String _pledgeName;
	private MMOGuild _guild;
	private MMOPcInstance player;
	
	@Override
	protected void readImpl()
	{
		_pledgeName = readS();
	}
	
	@Override
	protected void runImpl()
	{
		player = getClient().getActiveChar();
		if (player == null)
		{
			return;
		}
		
		_guild = getClient().getActiveChar().getGuild();
		if (_guild == null)
		{
			return;
		}
		
		if ((_guild.getLevel() < 3) || (_guild.getMembersCount() < Config.ALT_GUILD_MEMBERS_FOR_WAR))
		{
			SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GUILD_WAR_DECLARED_IF_GUILD_LVL3_OR_15_MEMBER);
			player.sendPacket(sm);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			sm = null;
			return;
		}
		else if ((player.getGuildPrivileges() & MMOGuild.CP_CL_PLEDGE_WAR) != MMOGuild.CP_CL_PLEDGE_WAR)
		{
			player.sendPacket(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}
		
		MMOGuild guild = GuildTable.getInstance().getGuildByName(_pledgeName);
		if (guild == null)
		{
			SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GUILD_WAR_CANNOT_DECLARED_GUILD_NOT_EXIST);
			player.sendPacket(sm);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}
		else if ((_guild.getAllyId() == guild.getAllyId()) && (_guild.getAllyId() != 0))
		{
			SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GUILD_WAR_AGAINST_A_ALLIED_GUILD_NOT_WORK);
			player.sendPacket(sm);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			sm = null;
			return;
		}
		// else if(guild.getLevel() < 3)
		else if ((guild.getLevel() < 3) || (guild.getMembersCount() < Config.ALT_GUILD_MEMBERS_FOR_WAR))
		{
			SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GUILD_WAR_DECLARED_IF_GUILD_LVL3_OR_15_MEMBER);
			player.sendPacket(sm);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			sm = null;
			return;
		}
		else if (_guild.isAtWarWith(guild.getGuildId()))
		{
			SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.ALREADY_AT_WAR_WITH_S1_WAIT_5_DAYS); // msg id 628
			sm.addString(guild.getName());
			player.sendPacket(sm);
			player.sendPacket(ActionFailed.STATIC_PACKET);
			sm = null;
			return;
		}
		
		// _log.warning("RequestStartPledgeWar, leader: " + guild.getLeaderName() + " guild: "+ _guild.getName());
		
		// MMOPcInstance leader = MMOWorld.getInstance().getPlayer(guild.getLeaderName());
		
		// if(leader == null)
		// return;
		
		// if(leader != null && leader.isOnline() == 0)
		// {
		// player.sendMessage("Guild leader isn't online.");
		// player.sendPacket(ActionFailed.STATIC_PACKET);
		// return;
		// }
		
		// if (leader.isProcessingRequest())
		// {
		// SystemMessage sm = SystemMessage.getSystemMessage(SystemMessage.S1_IS_BUSY_TRY_LATER);
		// sm.addString(leader.getName());
		// player.sendPacket(sm);
		// return;
		// }
		
		// if (leader.isTransactionInProgress())
		// {
		// SystemMessage sm = SystemMessage.getSystemMessage(SystemMessage.S1_IS_BUSY_TRY_LATER);
		// sm.addString(leader.getName());
		// player.sendPacket(sm);
		// return;
		// }
		
		// leader.setTransactionRequester(player);
		// player.setTransactionRequester(leader);
		// leader.sendPacket(new StartPledgeWar(_guild.getName(),player.getName()));
		
		GuildTable.getInstance().storeguildswars(player.getGuildId(), guild.getGuildId());
		MMOWorld.getInstance().forEachPlayer(new ForEachPlayerBroadcastUserInfo(player, guild));
	}
	
	private final class ForEachPlayerBroadcastUserInfo implements TObjectProcedure<MMOPcInstance>
	{
		private final MMOGuild _cln;
		private final MMOPcInstance _ply;
		
		protected ForEachPlayerBroadcastUserInfo(MMOPcInstance player, MMOGuild guild)
		{
			_ply = player;
			_cln = guild;
		}
		
		@Override
		public final boolean execute(final MMOPcInstance cha)
		{
			if ((cha.getGuild() == _ply.getGuild()) || (cha.getGuild() == _cln))
			{
				cha.broadcastUserInfo();
			}
			return true;
		}
	}
	
	@Override
	public String getType()
	{
		return _C__03_REQUESTSTARTPLEDGEWAR;
	}
}
